import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default ({
    state: {
        //用户id
        id: "",
        //是否登录
        isLogin: false,
        //token
        token: "",
        //用户信息
        userInfo: ""
    },
    //存储 state 数据
    mutations: {
        setId: function (state, id) {
            state.id = id
        },
        setIsLogin: function (state, isLogin) {
            state.isLogin = isLogin
        },
        setToken: function (state, token) {
            state.token = token
        },
        setUserInfo: function (state, userInfo) {
            state.userInfo = userInfo
        }
    },
    getters: {
        getId: (state) => {
            return state.id
        },
        getIsLogin: (state) => {
            return state.isLogin
        },
        getToken: (state) => {
            return state.token
        },
        getUserInfo: (state) => {
            return state.userInfo
        },
    },
    //异步设置
    actions: {
        /**
         * @param commit  调用 mutation的方法
         * @param state 就是我们定义的 state
         * @param info  就是调用该方法时的参数
         */
        updateId: (context, id) => {
            context.commit("setId", id)
        },
        updateIsLogin: (context, isLogin) => {
            context.commit("setIsLogin", isLogin)
        },
        updateToken: (context, token) => {
            context.commit("setToken", token)
        },
        updateUserInfo: (context, userInfo) => {
            context.commit("setUserInfo", userInfo)
        },
    },
    modules: {}
})